package com.bazhua.bazo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bazhua.bazo.repository.entity.AgentUser;
import com.bazhua.bazo.repository.entity.CheckUser;
import com.bazhua.bazo.repository.entity.ShopInfo;
import com.bazhua.bazo.repository.entity.WechatUser;
import com.bazhua.bazo.repository.mapper.AgentUserMapper;
import com.bazhua.bazo.repository.mapper.CheckUserMapper;
import com.bazhua.bazo.repository.model.CheckUserModel;
import com.bazhua.bazo.repository.model.UserType;
import com.bazhua.bazo.repository.model.result.CommonResult;
import com.bazhua.bazo.repository.model.result.PageListResult;
import com.bazhua.bazo.repository.model.result.ResultUtils;
import com.bazhua.bazo.service.IAgentUserService;
import com.bazhua.bazo.service.ICheckUserService;
import com.bazhua.bazo.service.IShopInfoService;
import com.bazhua.bazo.service.IWechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2021-03-02 0:31
 **/
@Service
public class AgentUserServiceImpl extends ServiceImpl<AgentUserMapper, AgentUser> implements IAgentUserService {

    @Autowired
    private IWechatUserService wechatUserService;

    public CommonResult registerAgentUser(String openId, Long userId) {
        WechatUser wechatUser = wechatUserService.findByOpenId(openId);
        if (wechatUser == null){
            return ResultUtils.error(90200, "用户不存在！");
        }
        if (wechatUser.getUserType() != UserType.User){
            return ResultUtils.error(90200, "非普通用户不能成为代理商业务员！");
        }
        registerAgentUser(wechatUser.getId(), userId);
        wechatUser.setUserType(UserType.Salesman);
        wechatUserService.updateById(wechatUser);
        return ResultUtils.ok();
    }

    public void registerAgentUser(Long mainUserId, Long userId) {
        AgentUser checkUser = new AgentUser();
        checkUser.setMainAgentUserId(mainUserId);
        checkUser.setAgentUserId(userId);
        checkUser.setCreateTime(new Date());
        baseMapper.insert(checkUser);
    }

    public List<Long> getUserIdsByAgentId(Long agentId) {
        QueryWrapper<AgentUser> queryWrapper = new QueryWrapper();
        List<Long> userIds = new ArrayList<>();
        WechatUser wechatUser = wechatUserService.findById(agentId);
        if (wechatUser.getUserType() == UserType.Agent) {
            queryWrapper.eq("main_agent_user_id", wechatUser.getId());
            List<AgentUser> agentUsers = baseMapper.selectList(queryWrapper);
            userIds = agentUsers.stream().map(x -> x.getAgentUserId()).collect(Collectors.toList());
            userIds.add(agentId);
        }else if (wechatUser.getUserType() == UserType.Salesman){
            userIds.add(agentId);
        }
        return userIds;
    }

    public PageListResult getPage(Integer pageNum, Integer pageSize, Long userId) {
        QueryWrapper<AgentUser> queryWrapper = new QueryWrapper();
        WechatUser wechatUser = wechatUserService.findById(userId);
        if (wechatUser != null && wechatUser.getUserType() == UserType.Salesman) {
            queryWrapper.eq("agent_user_id", wechatUser.getId());
        }
        Page<AgentUser> page = new Page<>(pageNum, pageSize);
        page = baseMapper.selectPage(page, queryWrapper);
        if (page.getRecords() != null && page.getRecords().size() > 0) {
            Page<CheckUserModel> modelPage = new Page<>();
            modelPage.setCurrent(page.getCurrent());
            modelPage.setSize(page.getSize());
            modelPage.setTotal(page.getTotal());
            modelPage.setPages(page.getPages());
            modelPage.setMaxLimit(page.getMaxLimit());
            List<CheckUserModel> list = new ArrayList<>();
            for (AgentUser agentUser : page.getRecords()) {
                CheckUserModel checkUserModel = new CheckUserModel();
                WechatUser user = wechatUserService.findById(agentUser.getAgentUserId());
                if (user == null)
                    continue;
                checkUserModel.setId(agentUser.getId());
                checkUserModel.setUserName(user.getUserName());
                list.add(checkUserModel);
            }
            modelPage.setRecords(list);
            return ResultUtils.pageOk(modelPage);
        }
        return ResultUtils.pageError(0, "查询数据为空");
    }

    public CommonResult deleteAgentUser(Long id) {
        baseMapper.deleteById(id);
        return ResultUtils.ok();
    }
}
